﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ITOMMS.Entity;
using ITOMMS.Business;
using Watson.Core.Tools;

namespace ITOMMS.Web.Finance
{
    public partial class AccountReceivableAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                    }
                }


                if (Request.Params["vid"] != null)
                {
                    string sid = Request.Params["vid"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                        btnSave.Visible = false;
                    }
                }
            }
        }
 


        private void DataShow(int troubleId)
        {
            tb往来单位应收账款Entity entity = new tb往来单位应收账款Entity();
            entity = entity.GetById(troubleId);

            if (entity != null)
            {
                ViewState["Entity"] = entity;

                txt科目编号.Text = entity.科目编号;
                txt部门.Text = entity.部门;
                txt单据编号.Text = entity.单据编号;
                txt经手人.Text = entity.经手人;
                txt科目名称.Text = entity.科目名称;
                txt金额.Text = entity.金额.ToString();
                txt合计.Text = entity.合计.ToString();
                txt付款方式.Text = entity.付款方式;


                if (entity.起始时间.HasValue)
                {
                    txt起始时间.Text = entity.起始时间.Value.ToShortDateString();
                }

                if (entity.终止时间.HasValue)
                {
                    txt终止时间.Text = entity.终止时间.Value.ToShortDateString();
                }


            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tb往来单位应收账款Entity entity = new ITOMMS.Entity.tb往来单位应收账款Entity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tb往来单位应收账款Entity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }

                entity.科目编号 = txt科目编号.Text;
                entity.部门 = txt部门.Text;
                entity.单据编号 = txt单据编号.Text;
                entity.经手人 = txt经手人.Text;
                entity.科目名称 = txt科目名称.Text;

                if (txt金额.Text.Trim() != string.Empty)
                {
                    entity.金额 = decimal.Parse(txt金额.Text.ToString());
                }


                if (txt合计.Text.Trim() != string.Empty)
                {
                    entity.合计 = decimal.Parse(txt合计.Text.ToString());
                }




                entity.付款方式 = txt付款方式.Text;





                DateTime startTime = new DateTime();
                if (DateTime.TryParse(txt起始时间.Text.Trim(), out  startTime))
                {
                    entity.起始时间 = startTime;
                }

                DateTime endTime = new DateTime();
                if (DateTime.TryParse(txt终止时间.Text.Trim(), out  endTime))
                {
                    entity.终止时间 = endTime;
                }

                if (entity.起始时间.HasValue && entity.终止时间.HasValue)
                {
                    if (entity.起始时间.Value > entity.终止时间.Value)
                    {
                        MessageboxInfo.MessageBox(this, MessageType.ERROR, "起始时间不能大于终止时间");
                        return;
                    }
                   
                }


                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));


                entity.SaveOrUpdate(entity);
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }

        }
 
 
    }
}
